/***************************************************************************
 *   Copyright (C) 2008 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#ifndef QTVOMP_H
#define QTVOMP_H

#include <QMainWindow>
#include <QButtonGroup>
#include <QStackedWidget>

#include "mediawidgetitem.h"
#include "recordingswidgetitem.h"
#include "channellistmodel.h"
#include "vdrthread.h"
#include "widgetplayvideo.h"
#include "widgetoptions.h"

class QLabel;
class QPushButton;
class QListView;
class QHostAddress;
class VDRThread;

class MainWindow : public QMainWindow
{
        Q_OBJECT

    public:
        /** Menu widgets */
        enum Menu
        {
            WIDGET_MENU = 0,
            WIDGET_CHANNELS,
            WIDGET_RECORDINGS,
            WIDGET_MEDIA,
            WIDGET_PLAYVIDEO,
            WIDGET_OPTIONS
        };

        MainWindow( QHostAddress server, QWidget *parent = 0 );

        void closeEvent ( QCloseEvent * event );

        QListView* channelListView() { return m_channelListView; };

        QItemSelectionModel* channelListSelectionModel() {return m_channelListSelectionModel; };

        QStackedWidget* centralWidget() { return m_centralWidget; };

        QTreeWidget* recordingsList() { return m_recordingsList; };

        QTreeWidget* mediaList() { return m_mediaList; };

    protected:
        void moveEvent ( QMoveEvent * event );
        void resizeEvent ( QResizeEvent * event );
        void hideEvent ( QHideEvent * event );
        void showEvent ( QShowEvent * event );

    signals:
        void setVDRServerAddress( QHostAddress vdr );
        void requestLogin();
        void getChannelList( quint32 );
        void getRecordingsList();
        void requestMediaList( const QString& dir );
        void requestOpenRecording( const QString& file );

    private slots:
        void displayMenu();
        void displayMenuTime();
        void displayChannelsTV( bool loadChannels = true );
        void displayChannelsProgramInfo();
        void displayRecordings( bool loadRecordings = true );
        void displayMediaList( bool loadMedia = true );
        void displayRadio();
        void displayMediaList( QTreeWidgetItem * item );
        void displayOptions();

        void updateChannels( const QList<channelEntry> & channelList );
        void updateRecordings( const QList<recordingsEntry> & recordingsList );
        void updateMediaList( const QList<mediaEntry> & mediaList );
        void updateServerOffset( qint32 offset );

    private:
        QHostAddress searchForServer( VDRThread * thread );
        void initMenuWidget();
        void initChannelsTVWidget();
        void initRecordingsWidget();
        void initMediaWidget();
        void initOptionsWidget();

        //Recursive helperfunction
        void addRecordingsWidgetItem( const QString & text, const QString & fileName,
                                      int start, QDateTime dateTime, RecordingsWidgetItem * parentItem );

        QStackedWidget* m_centralWidget;
        QWidget* m_menuWidget;
        QWidget* m_mediaWidget;
        QWidget* m_channelsTVWidget;
        QWidget* m_recordingsWidget;
        WidgetOptions* m_optionsWidget;
        WidgetPlayVideo* m_widgetPlayVideo;

        QListView *m_channelListView;
        ChannelListModel *m_channelListModel;
        QItemSelectionModel *m_channelListSelectionModel;
        QList<QString> m_channelListList;

        QTreeWidget* m_mediaList;
        MediaWidgetItem* m_mediaListItem;

        QTreeWidget* m_recordingsList;
        RecordingsWidgetItem* m_recordingsItem;

        QLabel* m_menuTimeLabel;

        VDRThread m_vdrThread;
        VDR * m_vdr;
        qint32 m_serverTimeOffset;
};

#endif
